﻿<?php
	require_once('../system.php');

	$order = $_GET['o'];
	
	$system = new System();
	$playlistManager = $system->playlistManager;
	
	// Se inicia sesión para recuperar el id del usuario
	if (!$system->userManager->logged())
		@session_start();
	
	switch($order) {
		case 'getSongsId':
			$playlistId = $_GET['id'];
			$playlist = $playlistManager->getPlaylist($playlistId);
			
			$found = ($playlist == null) ? 'false' : 'true';
			
			$last_song = end($playlist->songs);
			echo '{"found" : "' . $found . '", ';
			if ($playlist != null) {
					echo '"songs" : [';
					foreach ($playlist->songs as $song) {
						echo '{"player" : ' . $song->player . ', ';
						echo '"idVideo" : "' . $song->idVideo . '"}';
						if ($song != $last_song)
							echo ', ';
					}
					echo ']';
				}
				echo '}';
		break;
		
		case 'view_my_playlists':
			echo $playlistManager->getMyPlaylistsJSON();
		break;
		
		case 'view_my_favorites':
			echo $playlistManager->getMyFavoritesJSON();
		break;
		
		case 'positiveVote':
			$playlistId = $_GET['id'];
			$userId = $_SESSION['id'];
			if ($playlistManager->canVote($userId,$playlistId)) {
				$positiveVotes = $playlistManager->positiveVote($userId,$playlistId);
				$found = ($positiveVotes == null) ? 'false' : 'true';
				echo '{"found" : "' . $found . '", ';
				echo '"positiveVotes" : ' . $positiveVotes . '}';;
			}
			else {
				echo '{"found" : "false"} ';
			}
		break;
		
		case 'negativeVote':
			$playlistId = $_GET['id'];
			$userId = $_SESSION['id'];
			if ($playlistManager->canVote($userId,$playlistId)) {
				$negativeVotes = $playlistManager->negativeVote($userId,$playlistId);
				$found = ($negativeVotes == null) ? 'false' : 'true';
				echo '{"found" : "' . $found . '", ';
				echo '"negativeVotes" : ' . $negativeVotes . '}';;
			}
			else {
				echo '{"found" : "false"} ';
			}
		break;
		
		case 'addFavorite':
			$playlistId = $_GET['id'];
			$userId = $_SESSION['id'];
			$success = ($system->database->playlistDatabase->addFavorite($userId, $playlistId)) ? 
						'true' :
						'false';
			
			echo '{"success" : "' . $success . '"}';
			
			break;
			
		case 'deleteFavorite':
			$playlistId = $_GET['id'];
			$userId = $_SESSION['id'];
			$success = ($system->database->playlistDatabase->removeFavorite($userId, $playlistId)) ? 
						'true' :
						'false';
						
			echo '{"success" : "' . $success . '"}';
			
			break;
			
		case 'viewPlaylist':
			$playlistId = $_GET['id'];
			$userId = (isset($_SESSION['id'])) ? $_SESSION['id'] : -1;
			$playlist = $playlistManager->getPlaylist($playlistId);
			
			$found = ($playlist == null) ? 'false' : 'true';
			$letVote = $playlistManager->canVote($userId,$playlistId);
			$letVote = $letVote == "1" ? 'true' : 'false';
			
			$isFavorite = ($playlistManager->isFavorite($userId, $playlistId)) ? 
							'true' : 
							'false';
			
			/* Se construye el objeto JSON de retorno con:
				* Booleano que indica si se ha encontrado la información
					de la playlist.
				* Información de la playlist encontrada:
					* id
					* nombre
					* votos positivos/negativos/puedeVotar?
					* es favorita?
					* canciones
			*/
			echo '{"found" : "' . $found . '"';
			if ($playlist != null) {
				$last_song = end($playlist->songs);
				echo ', "playlist" : ';
					echo '{"id" : ' . $playlist->id . ', ';
					echo '"name" : "' . $playlist->name . '",';
					echo '"positiveVotes" : "'. $playlist->positiveVotes .'",';
					echo '"negativeVotes" : "'. $playlist->negativeVotes .'",';
					echo '"letVote" : "'. $letVote .'",';
					echo '"isFavorite" : "' . $isFavorite . '",';
					echo '"songs" : [';
					foreach ($playlist->songs as $song) {
						echo '{"id" : ' . $song->id . ', ';
						echo '"title" : "' . $song->title . '"}';
						if ($song != $last_song)
							echo ', ';
					}
					echo ']}';
				}
				echo '}';
		break;
		
		case 'showCreatePlaylistMenu':
			$playlistManager->showCreatePlaylistMenu();
		break;
		
		case 'showEditPlaylistMenu':
			$playlistId = $_GET['id'];
			$playlist = $system->database->playlistDatabase->getPlaylistById($playlistId);
			$playlist->songs = $system->database->songDatabase->getAllSongsFromPlaylist($playlistId);
			$playlistManager->showEditPlaylistMenu($playlist);
		break;
		
		case 'createPlaylist':
			$name = $_GET['name'];
			$songs = $_GET['songs'];
			// Se construye el objeto de la clase Playlist
			$playlist = new Playlist();
			$playlist->creatorId = $_SESSION['id'];
			$playlist->name = $name;
			for ($i = 0; $i < count($songs); $i += 2) {
				$song = new Song();
				$song->title = $songs[$i];
				$song->link = $songs[$i + 1];
				$playlist->addSong($song);
			}
			$playlistManager->createPlaylist($playlist);
			
			echo $playlistManager->getMyPlaylistsJSON();
		break;
		
		case 'editPlaylist':
			$id = $_GET['id'];
			$name = $_GET['name'];
			$songs = $_GET['songs'];
			
			$newPlaylist = new Playlist();
			$newPlaylist->id = $id;
			$newPlaylist->creatorId = $_SESSION['id'];
			$newPlaylist->name = $name;
			for ($i = 0; $i < count($songs); $i += 2) {
				$song = new Song();
				$song->title = $songs[$i];
				$song->link = $songs[$i + 1];
				$newPlaylist->addSong($song);
			}
			
			$playlistManager->editPlaylist($newPlaylist);
			
			echo $playlistManager->getMyPlaylistsJSON();
		break;
		
		case 'deletePlaylist':
			$id = $_GET['id'];
			$playlistManager->deletePlaylist($id);
			
			echo $playlistManager->getMyPlaylistsJSON();
		break;
	}
?>